package com.utbm.lo54.controllers;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utbm.lo54.dao.mysql.MysqlCourseDAO;
import com.utbm.lo54.dao.mysql.MysqlLocationDAO;
import com.utbm.lo54.dao.mysql.MysqlSessionDAO;
import com.utbm.lo54.entity.Course;
import com.utbm.lo54.entity.Location;
import com.utbm.lo54.entity.Session;

/**
 * Servlet implementation class CatalogueServlet
 */
@WebServlet("/CatalogueServlet")
public class CatalogueServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CatalogueServlet() {
		super();


		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MysqlCourseDAO courseDAO = new MysqlCourseDAO();
		MysqlLocationDAO locationDAO = new MysqlLocationDAO();

		request.setAttribute("courses",courseDAO.getAllCourse());
		request.setAttribute("locations",locationDAO.getAllLocation());
		RequestDispatcher dis=request.getRequestDispatcher("Catalogue.jsp");
		dis.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MysqlCourseDAO courseDAO = new MysqlCourseDAO();
		MysqlLocationDAO locationDAO = new MysqlLocationDAO();
		MysqlSessionDAO sessionDAO = new MysqlSessionDAO();

		Set<Course> courses = new HashSet<Course>();
		Set<Course> dbCourses = courseDAO.getAllCourse();
		Set<Location> locations = locationDAO.getAllLocation();

		String key =request.getParameter("key");
		String time = request.getParameter("date");
		String city = request.getParameter("location");
		Date date = null ;
		Location location = null;
		//if(time!=null) date= new Date(time);
		if(city.equalsIgnoreCase("All")) city = null;
		if(time.isEmpty())time=null;
		if(key.isEmpty())key=null;

		if(city!=null) location = locationDAO.findByCity(city);

		SimpleDateFormat ft = new SimpleDateFormat ("dd/MM/yy"); 

		try { 
			if(time!=null)
			date = ft.parse(time); 

		} catch (ParseException e) { 
			time = null;
		}

		if( city==null && key==null && time==null){
			courses = dbCourses;
		}
		else{
			for(Course course : dbCourses){
				if(key!=null && !course.getTitle().contains(key)) continue;
				else if(time!=null || city!=null){
					Set<Session> sessions = sessionDAO.findByCourse(course.getCode());
					if(sessions==null)continue;
					Boolean found = false;
					Iterator<Session> it = sessions.iterator();
					while(!found && it.hasNext()){
						Boolean filter = true;
						Session session = (Session) it.next();
						if(city!=null && session.getLocationId() != location.getId())	filter= false;
						else if(time!=null && (date.compareTo(session.getStartDate())<0  || date.compareTo(session.getEndDate())>0 )) filter = false;

						if(filter && !courses.contains(course)){
							courses.add(course);
							found=true;
						}

					}
				}
				else courses.add(course);
			}
		}

		request.setAttribute("courses",courses);
		request.setAttribute("locations",locations);
		RequestDispatcher dis=request.getRequestDispatcher("Catalogue.jsp");
		dis.forward(request,response);
	}

}
